package logic

import (
	"context"
	"encoding/json"
	"errors"
	"shop/cache"
	"shop/model/mysql"
	"time"

	"shop/internal/svc"
	"shop/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopAddLogic) ShopAdd(in *shop.ShopAddRequest) (*shop.ShopAddResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var h mysql.House
	h.UserId = int(in.UserId)
	h.Image = in.Image
	h.Info = in.Info
	h.Price = float64(in.Price)
	h.Num = int(in.Num)
	h.Title = in.Title
	var a mysql.Auther
	if err = a.GetByUserId(); err != nil {
		logx.Error("用户未实名禁止发布房源")
		return nil, errors.New("用户未实名禁止发布房源")
	}
	if err = h.Create(); err != nil {
		logx.Error("添加成功")
		return nil, errors.New("添加成功")
	}
	marshal, err := json.Marshal(a)
	if err != nil {
		return nil, err
	}
	cache.Rdb.Set(l.ctx, "shopAdd", marshal, time.Minute*5)
	return &shop.ShopAddResponse{
		Id: int64(h.ID),
	}, nil
}
